package com.qst.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.beans.Category;
import com.qst.beans.CategorySecond;
import com.qst.beans.PageUtils;
import com.qst.beans.Product;
import com.qst.dao.CategoryMapper;
import com.qst.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper mapper ;

    @Override
    public List<Category> findEight() {
        return mapper.findEight();
    }

    @Override
    public List<Category> findAll() {
        return mapper.findAll();
    }

    @Override
    public PageUtils<Product> findByCid(Integer page, Integer cid) {
        PageUtils<Product> utils = new PageUtils<>();
        PageHelper.startPage(page,8);
        List<Product> csByCid = mapper.findByCid(cid);//商品一级分类
        PageInfo<Product> pageInfo = new PageInfo(csByCid);
        utils.setPage(page);
        utils.setList(csByCid);
        utils.setTotalPage(pageInfo.getPages());
        return utils;
    }

    @Override
    public List<CategorySecond> findCSByCid(Integer cid) {
        return mapper.findCSByCid(cid);
    }

    @Override
    public PageUtils<Product> findByCSid(Integer csid, Integer page) {
        PageUtils<Product> utils = new PageUtils<>();
        if (page==null||page==0)
            page=1;
        PageHelper.startPage(page,8);
        List<Product> csByCSid = mapper.findByCSid(csid);
        PageInfo<Product> pageInfo = new PageInfo(csByCSid);
        utils.setPage(page);
        utils.setList(csByCSid);
        utils.setTotalPage(pageInfo.getPages());
        return utils;
    }

    @Override
    public int addCategory(Category category) {
        return mapper.addCategory(category);
    }

    @Override
    public int deletByid(Integer id) {
        return mapper.deleteByid(id);
    }

    @Override
    public Category findCByid(Integer id) {
        return mapper.findCByid(id);
    }

    @Override
    public int saveCategory(Category category) {
        return mapper.saveCategory(category);
    }


}
